#include "my_headerT2csJ.h"

void* workfunc(void *ret){
    threadPoll_t * tpt = (threadPoll_t *) ret;

    pthread_mutex_lock(&tpt->pmt);
    MYSQL * mysql = mysql_init(NULL);
    MYSQL* mrc =  mysql_real_connect(mysql,DATABASE_IP,DATABASE_ACCOUNT,DATABASE_PASSWORD,DATABASE_NAME,0,NULL,0);
    if(mrc == NULL){
        printf("%s,%s\n","mysql_real_connect",mysql_error(mysql));
        exit(-1);
    }
    pthread_mutex_unlock(&tpt->pmt);


    /* ClientSession css; */
    /* memset(&css,0,sizeof(css)); */
    /* char user_name[64] = "李四"; */
    /* init_session(&css,mysql,user_name,-1); */
    /* printf("nowid:%d,nowcdr:%s\n",css.pcn_id,css.current_dir); */




    while(1){
        pthread_mutex_lock(&tpt->pmt);
        while(tpt->flag == 0 && tpt->ltt.list_length <= 0){
            printf("thread %ld wait...\n",pthread_self());
            pthread_cond_wait(&tpt->pct,&tpt->pmt);
        }
        if(tpt->flag == 1){
            printf("thread %ld gone...\n",pthread_self());
            pthread_mutex_unlock(&tpt->pmt);
            pthread_exit(NULL);
        }

        int netfd = tpt->ltt.head->val;
        printf("thread %ld get netfd %d...\n",pthread_self(),tpt->ltt.head->val);
        headDel(&tpt->ltt);
        pthread_mutex_unlock(&tpt->pmt);




        //css初始化应该可以放在这，也可以顺便处理上次用户的残留
        ClientSession css;
        memset(&css,0,sizeof(css));
        char get_user_name[64] = {0};
        strcpy(get_user_name,"李四");
        init_session(&css,mysql,get_user_name,netfd);
        printf("nowid:%d,nowcdr:%s\n",css.pcn_id,css.current_dir);

        /* css.netfd = netfd; */
        /* css.mysql = mysql; */
        while(1){
            char bufrecv[MAX_PATH_LEN] = {0};
            int ret = recv(css.netfd,bufrecv,sizeof(bufrecv),0);
            if(!ret){
                printf("the C quit...\n");
                break;
            }
            // 使用sscanf分割字符串
            char optname[64] = {0};
            char pathrecv[MAX_PATH_LEN] = {0};
            sscanf(bufrecv, "%s %s", optname,pathrecv);
            if(strcmp("cd",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,1,pathrecv);
            }else if (strcmp("ls",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,2,pathrecv);
            }else if(strcmp("pwd",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,3,pathrecv);
            }else if (strcmp("mkdir",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,4,pathrecv);
            }else if(strcmp("rmdir",optname) == 0){
                printf("recvOpt:%s\n",optname);
                opt_dir(&css,5,pathrecv);
            }else{
                send(netfd,"!Some Error...",15,MSG_NOSIGNAL);
                printf("wrongOpt?\n");
                /* break; //现在输错直接退出循环不服务这个客户端了 */
            }
        }





        /* trans_file(netfd); */

        printf("thread %ld done...\n",pthread_self());
        close(netfd);
    }
    return 0;
}

int make_worker(int workernum,threadPoll_t *tpt){
    for(int i = 0;i<workernum;i++){
        pthread_create(&tpt->wkt.workers[i],NULL,workfunc,tpt);
    }
    return 0;
}

